package com.sleeping.mall.common.encryption;

import com.sleeping.mall.common.utils.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

public class Base64Utils {
    static final Base64.Encoder encoder = Base64.getEncoder();
    static final Base64.Decoder decoder = Base64.getDecoder();

    /**
     * base解码
     *
     * @param text
     * @return
     */
    public static String toDecodeBase64(String text) {
        try {
            if (StringUtils.isNotEmpty(text)) {
                return new String(decoder.decode(text), "UTF-8");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * base编码
     *
     * @param text
     * @return
     */
    public static String toEncodeBase64(String text) {

        try {
            if (StringUtils.isNotEmpty(text)) {
                final byte[] textByte = text.getBytes("UTF-8");
                return encoder.encodeToString(textByte);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
